package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.ExamStudent;
import nsu.edu.zsq.bean.ExamTeacher;
import nsu.edu.zsq.bean.SchExam;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.bean.Teacher;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchExamService;
import nsu.edu.zsq.service.sys.SchMajorService;

/** 
* Description: 考试的controller<br>
*/
@RequestMapping("/sys/exam")
@Controller
public class SchExamController {
    
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    @Autowired
    private SchExamService schExamService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取考试课程列表
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchExam")
    @ResponseBody
    public JsonData listSchExam(String majorNo, PageQuery pageQuery) {
        PageInfo<SchExam> listSchExam = schExamService.listSchExamByMajorNoWithPage(majorNo, pageQuery);
        return JsonData.success(listSchExam);
    }
    
    /**
     * 跳转到添加考试课程页面
     * @param majorNo 专业代码
     * @param model
     * @return String manager/exam/exam_addCourse.html
     */
    @RequestMapping("/toAddStuCourse")
    public String toListStuCourse(@RequestParam("majorNo")String majorNo, Model model) {
        model.addAttribute("majorNo", majorNo);
        return "manager/exam/exam_addCourse.html";
    }
    
    /**
     * 根据专业代码获取所有教授的课程
     * @param majorNo 专业代码
     * @return JsonData
     */
    @RequestMapping("/listStuCourse")
    @ResponseBody
    public JsonData listStuCourse(@RequestParam("majorNo")String majorNo) {
        List<HashMap<String, String>> listStuCourseByMajorNo = schExamService.listStuCourseByMajorNo(majorNo);
        return JsonData.success(listStuCourseByMajorNo);
    }
    
    /**
     * 添加课程到考试表中
     * @param courseNos 课程代码数组
     * @param majorNo 专业代码
     * @return JsonData
     */
    @RequestMapping("/saveCourseToTchExam")
    @ResponseBody
    public JsonData saveCourseToTchExam(@RequestParam("courseNos[]")String[] courseNos, @RequestParam("majorNo")String majorNo) {
        schExamService.saveCourseToSchExam(courseNos, majorNo);
        return JsonData.success();
    }
    
    /**
     * 删除考试课程
     * @param schExamIds 考试id数组
     * @return JsonData 
     */
    @RequestMapping("/delSchExam")
    @ResponseBody
    public JsonData delSchExam(@RequestParam("schExamIds[]")int[] schExamIds) {
        schExamService.deleteCourseFromSchExam(schExamIds);
        return JsonData.success();
    }
    
    /**
     * 跳转到编辑考试课程时间的页面
     * @param schExamId 考试id
     * @param model
     * @return String manager/exam/exam_editTime.html
     */
    @RequestMapping("/toEditSchExamTime")
    public String toEditSchExamTime(@RequestParam("schExamId")int schExamId, Model model) {
        SchExam schExam = schExamService.getSchExam(schExamId);
        model.addAttribute("schExam", schExam);
        return "manager/exam/exam_editTime.html";
    }
    
    /**
     * 更新考试课程
     * @param schExam 考试课程信息
     * @return JsonData 
     */
    @RequestMapping("/updateSchExamTime")
    @ResponseBody
    public JsonData updateSchExamTime(SchExam schExam) {
        schExamService.updateSchExam(schExam);
        return JsonData.success();
    }
    
    /**
     * 跳转到考试管理页面
     * @param schExamId 考试id
     * @param model
     * @return String manager/exam/exam_manager.html
     */
    @RequestMapping("/toExamManager")
    public String toExamManager(@RequestParam("schExamId")int schExamId, Model model) {
        model.addAttribute("schExamId", schExamId);
        return "manager/exam/exam_manager.html";
    }
    
    /**
     * 获取一条考试课程信息
     * @param schExamId 考试id
     * @return JsonData
     */
    @RequestMapping("/getSchExamWithRoom")
    @ResponseBody
    public JsonData getSchExamWithRoom(@RequestParam("schExamId")int schExamId) {
        Map<String, Object> map = schExamService.getSchExamWithRoom(schExamId);
        return JsonData.success(map);
    }
    
    /**
     * 添加考试教室
     * @param schExam
     * @return JsonData
     */
    @RequestMapping("/saveExamRoom")
    @ResponseBody
    public JsonData saveExamRoom(SchExam schExam) {
        schExamService.saveExamRoom(schExam);
        return JsonData.success();
    }
    
    /**
     * 删除考试教室
     * @param schExamId 考试id
     * @return JsonData
     */
    @RequestMapping("/delExamRoom")
    @ResponseBody
    public JsonData delExamRoom(@RequestParam("schExamId")int schExamId, @RequestParam("roomNames[]")String[] roomNames) {
        schExamService.delExamRoom(schExamId, roomNames);
        return JsonData.success();
    }
    
    // =======================================监考教师==========================================
    
    /**
     * 跳转到监考教师页面
     * @param schExamId 考试id
     * @param majorNo 专业代码
     * @param model
     * @return String manager/exam/exam_manager_examTeacher.html
     */
    @RequestMapping("/toEditExamTeacher")
    public String toEditExamTeacher(SchExam schExam, Model model) {
        model.addAttribute("schExam", schExam);
        return "manager/exam/exam_manager_examTeacher.html";
    }
    
    /**
     * 获取监考教师信息
     * @param schExam 考试课程信息（id,roomName）
     * @return JsonData
     */
    @RequestMapping("/listExamTeacher")
    @ResponseBody
    public JsonData listExamTeacher(SchExam schExam) {
        List<ExamTeacher> listExamTeacher = schExamService.listExamTeacher(schExam);
        return JsonData.success(listExamTeacher);
    }
    
    /**
     * 跳转到添加监考教师页面
     * @param majorNo 专业代码
     * @param schExamId 考试id
     * @param roomName 监考教室
     * @param model
     * @return String manager/exam/exam_manager_addExamTeacher.html
     */
    @RequestMapping("/toAddExamTeacher")
    public String toAddExamTeacher(@RequestParam("majorNo")String majorNo, 
            @RequestParam("schExamId")int schExamId,
            @RequestParam("roomName")String roomName, Model model) {
        model.addAttribute("majorNo", majorNo);
        model.addAttribute("schExamId", schExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/exam_manager_addExamTeacher.html";
    }
    
    /**
     * 根据专业代码获取专业所在院系的所有教师信息
     * @param majorNo 专业代码
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listTeacherByMajorNoWithPage")
    @ResponseBody
    public JsonData listTeacherByMajorNoWithPage(@RequestParam("majorNo")String majorNo, PageQuery pageQuery) {
        PageInfo<Teacher> listTeacherByMajorNoWithPage = schExamService.listTeacherByMajorNoWithPage(majorNo, pageQuery);
        return JsonData.success(listTeacherByMajorNoWithPage);
    }
    
    /**
     * 添加监考教师
     * @param examTeacher 监考教师信息
     * @param teacherNos 教师编号数组
     * @return JsonData
     */
    @RequestMapping("/saveTeacherToExam")
    @ResponseBody
    public JsonData saveTeacherToExam(ExamTeacher examTeacher, @RequestParam("teacherNos[]")String[] teacherNos) {
        schExamService.saveTeacherToExam(examTeacher, teacherNos);
        return JsonData.success();
    }
    
    /**
     * 删除监考教师
     * @param examTeacherIds 监考教师id数组
     * @return JsonData
     */
    @RequestMapping("/delExamTeacher")
    @ResponseBody
    public JsonData delExamTeacher(@RequestParam("examTeacherIds[]")int[] examTeacherIds) {
        schExamService.delExamTeacher(examTeacherIds);
        return JsonData.success();
    }
    
    // =======================================考试学生==========================================
    
    /**
     * 跳转到监考教师页面
     * @param schExam 考试课程信息（id,roomName）
     * @param model
     * @return String manager/exam/exam_manager_examStudent.html
     */
    @RequestMapping("/toEditExamStudent")
    public String toEditExamStudent(SchExam schExam, Model model) {
        model.addAttribute("schExam", schExam);
        return "manager/exam/exam_manager_examStudent.html";
    }
    
    /**
     * 获取考试学生信息
     * @param schExam 考试课程信息（id,roomName）
     * @return JsonData
     */
    @RequestMapping("/listExamStudent")
    @ResponseBody
    public JsonData listExamStudent(SchExam schExam) {
        List<ExamStudent> listExamStudent = schExamService.listExamStudent(schExam);
        return JsonData.success(listExamStudent);
    }
    
    /**
     * 跳转到添加考试学生页面
     * @param courseNo 课程代码
     * @param schExamId 考试id
     * @param roomName 监考教室
     * @param model
     * @return String manager/exam/exam_manager_addExamStudent.html
     */
    @RequestMapping("/toAddExamStudent")
    public String toAddExamStudent(@RequestParam("courseNo")String courseNo, 
            @RequestParam("schExamId")int schExamId,
            @RequestParam("roomName")String roomName, Model model) {
        model.addAttribute("courseNo", courseNo);
        model.addAttribute("schExamId", schExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/exam_manager_addExamStudent.html";
    }
    
    /**
     * 根据课程代码获取课程下所有的学生信息
     * @param courseNo 课程代码
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listStudentByMajorNoWithPage")
    @ResponseBody
    public JsonData listStudentByMajorNoWithPage(@RequestParam("courseNo")String courseNo, PageQuery pageQuery) {
        PageInfo<Student> listStudentByMajorNoWithPage = schExamService.listStudentByMajorNoWithPage(courseNo, pageQuery);
        return JsonData.success(listStudentByMajorNoWithPage);
    }
    
    /**
     * 添加考试学生
     * @param examStudent考试学生信息
     * @param stuNos 学号数组
     * @return JsonData
     */
    @RequestMapping("/saveStudentToExam")
    @ResponseBody
    public JsonData saveStudentToExam(ExamStudent examStudent, @RequestParam("stuNos[]")String[] stuNos) {
        schExamService.saveStudentToExam(examStudent, stuNos);
        return JsonData.success();
    }
    
    /**
     * 删除考试学生
     * @param examStudentIds 考试学生id数组
     * @return JsonData
     */
    @RequestMapping("/delExamStudent")
    @ResponseBody
    public JsonData delExamStudent(@RequestParam("examStudentIds[]")int[] examStudentIds) {
        schExamService.delExamStudent(examStudentIds);
        return JsonData.success();
    }
    
    /**
     * 自动排座位号
     * @param examStudent 考试学生信息
     * @return JsonData
     */
    @RequestMapping("/autoSeatNumber")
    @ResponseBody
    public JsonData autoSeatNumber(ExamStudent examStudent) {
        schExamService.autoSeatNumber(examStudent);
        return JsonData.success();
    }
}
